/**
 * Created by bai on 2015/7/11.
 */

var Service = function(){
    /*
     * 任务界面服务列表
     * */
    var taskServiceList  = function(fun){
        var options = {};
        options.successFunction = function (data) {
            $('.taskMainServiceList').html('');
            fun();
            //获得接受完成的服务
            var options = {};
            options.startDay = "";
            options.endDay ="";
            options.StatusType=["complete"]
            options.successFunction = function (dataC) {
                var options = {};
                options.successFunction = function (dataN) {
                    $('.taskMainServiceList').html('');
                    //加菜提醒
                    $.each(dataN.data[0],function(k,v){
                        var time=new Date(v.newLabel.time.sec*1000);
                        time=time.getHours()+':'+time.getMinutes();
                        $('<dl  data-function="btn_taskNotAcceptNewLineItem" data-id="'+v.newLabel.orderId+'">\
                   <div class="listImg" data-function="btn_taskNotAcceptNewLineItem"></div>\
                       <dt class="contTitle" data-function="btn_taskNotAcceptNewLineItem">'+ v.newLabel.Label+'</dt>\
                       <dd class="contArtitle" data-function="btn_taskNotAcceptNewLineItem">加餐处理，'+v.newLabel.Item.join("、")+'</dd>\
                       <div class="time" data-function="btn_taskNotAcceptNewLineItem">'+time+'</div>\
                       <div class="contDot" style="display: none" data-function="btn_taskNotAcceptNewLineItem"></div>\
                   </dl>').appendTo('.taskMainServiceList');
                    })
                    //未接受的服务
                    $.each(data.data[0].new,function(k,v){
                        var time=new Date(v.time.sec*1000);
                        time=time.getHours()+':'+time.getMinutes();
                        var tId =v.TId;
                        OrderingSystem.getTableById(tId, function (data) {
                            $('<dl  data-function="btn_taskNotAcceptServiceList" data-id="'+v.id+'">\
                   <div class="listImg" data-function="btn_taskNotAcceptServiceList"></div>\
                       <dt class="contTitle" data-function="btn_taskNotAcceptServiceList">'+data.data[0].label+'</dt>\
                       <dd class="contArtitle" data-function="btn_taskNotAcceptServiceList">'+v.detail+'</dd>\
                       <div class="time" data-function="btn_taskNotAcceptServiceList">'+time+'</div>\
                       <div class="contDot" data-function="btn_taskNotAcceptServiceList"></div>\
                   </dl>').appendTo('.taskMainServiceList');
                        });

                    })
                    //已经完成的服务
                    $.each(dataC.data,function(k,v){

                        var time=new Date(v.time.sec*1000);
                        time=time.getHours()+':'+time.getMinutes();
                        var tId =v.TId;
                        OrderingSystem.getTableById(tId, function (data) {
                            $('<dl data-function="btn_taskAcceptServiceList" data-id="'+v.id+'">\
                   <div class="listImg"  data-function="btn_taskAcceptServiceList"></div>\
                       <dt class="contTitle" data-function="btn_taskAcceptServiceList">'+data.data[0].label+'</dt>\
                       <dd class="contArtitle" data-function="btn_taskAcceptServiceList">'+v.detail+'</dd>\
                       <div class="time" data-function="btn_taskAcceptServiceList">'+time+'</div>\
                       <div class="contDot" style="display: none" data-function="btn_taskAcceptServiceList"></div>\
                   </dl>').appendTo('.taskMainServiceList');
                        });

                    })
                };
                OrderingSystem.getTableByNormalNewLineItem(options);

            };
            OrderingSystem.getLoginAfterwardsCompleteService(options);


        };
        OrderingSystem.getMyNotCompleteServiceList(options);
    }
    /*
     * 加餐提醒
     * */
    var notAcceptNewLineItem = function(obj,fun){
        var oId="";
        if(obj.parents('dl').data('id')){
            oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }
        OrderingSystem.getOrderById(oId, function (data) {
            var name = "";
            var ResMobile = "";
            var specialRequest = "无";
            if(data.data[0].reserve == null){
                name = "无";
                ResMobile = "无"
            }
            else{
                name = data.data[0].reserve.name;
                ResMobile = data.data[0].reserve.ResMobile;
            }
            if(data.data[0].request.specialRequest){
                specialRequest = data.data[0].request.specialRequest;
            }
            $('#subAddDishesHandlePage .subAddDishesHandlePageCont').html('');
            fun();
            $('<p class="title">点餐处理</p>\
               <div class="personContactInfo">\
                   <p class="contactWay">联系方式</p>\
                   <div class="personContact">\
                       <p class="personName">姓名：'+name+'</p>\
                       <p class="phoneNum">电话：'+ResMobile+'</p>\
                       <a class="phoneIco" href="tel:'+ResMobile+'"></a>\
                   </div>\
               </div>\
               <ul class="serviceList">\
                   <li class="serviceInfo">\
                       <p class="serviceTitle">服务内容</p>\
                       <p class="serviceCont">点餐处理</p>\
                   </li>\
                   <li class="serviceInfo">\
                       <p class="serviceTitle">支付状态</p>\
                       <p class="serviceCont">未支付</p>\
                   </li>\
                   <li class="serviceInfo">\
                       <p class="serviceTitle">特殊需求</p>\
                       <p class="serviceCont demand">'+specialRequest+'</p>\
                   </li>\
               </ul>\
               <ul class="addDishesList"></ul>').appendTo('#subAddDishesHandlePage .subAddDishesHandlePageCont');
            $.each(data.data[0].itemList,function(k,v){
                if(v.status == "new"){
                    $('<li class="addDishesInfo">\
                           <p class="dishName">'+ v.name+'</p>\
                           <p class="dishCount">×'+ v.count+'</p>\
                           <p class="dishPrice">￥'+ (v.count* v.price/100).toFixed(2)+'</p>\
                       </li>').appendTo('#subAddDishesHandlePage .addDishesList');
                }
            });
            $('#subAddDishesHandlePage .subAddDishesHandlePageBottom').html('');
            $('<div class="printMenuBtn" data-function="btn_printNewLineItem" data-id="'+oId+'">打印菜单</div>').appendTo('#subAddDishesHandlePage .subAddDishesHandlePageBottom');
        })
    }
    /*
     * 点击打印菜单按钮
     * */
    var printNewLineItem = function(obj,fun){
        var oId = obj.data('id');
        OrderingSystem.orderLineItemPrinter(oId, function (data) {
            setTimeout(function(){fun()},500)
        });
    }
    /*
     * 未接受的服务详情
     * */
    var notAcceptServiceInfo = function(obj,fun){
        var oId="";
        if(obj.parents('dl').data('id')){
            oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }

        var options = {};
        options.SCId=oId;
        options.successFunction = function (dataS) {
            $('#subcallSeverPage .seviceInfo').html('');
            fun();
            //alert(JSON.stringify(data))
            $('<div class="btm">\
                <div class="acceptTask" data-function="btn_accpetServiceCall" data-id="'+oId+'">接受任务</div>\
                </div>').prependTo('#subcallSeverPage .seviceInfo');
            var tId =dataS.data[0].TId;
            OrderingSystem.getTableById(tId, function (data) {
                var time=new Date(data.data[0].time.sec*1000);
                time=time.getHours()+':'+time.getMinutes();
                $('<div class="cont">\
                <ul class="contList">\
                <li class="deskNum">\
                    <div></div>\
                    <p>'+data.data[0].label+'</p>\
                </li>\
                <li class="whoCall">\
                <div>联系方式</div>\
                <p>'+data.data[0].label+'</p>\
                </li>\
                    <li class="severInfo">\
                        <div>服务内容</div>\
                        <p class="callPaper">'+dataS.data[0].detail+'</p>\
                        <p class="callTime">'+time+'呼叫服务</p>\
                        <p class="acceptCall">--:--接受服务</p>\
                        <p class="finishCall">--:--完成服务</p>\
                    </li>\
                    <li class="accptOrNot">\
                        <div>接受状态</div>\
                        <p>任务未接受</p>\
                    </li>\
                    <li class="severOrNot">\
                        <div>服务状态</div>\
                        <p>服务未确认</p>\
                    </li>\
                </ul>\
                </div>').prependTo('#subcallSeverPage .seviceInfo')
                /*     $('<dl  data-function="btn_taskNotAcceptServiceList">\
                 <div class="listImg" data-function="btn_taskNotAcceptServiceList"></div>\
                 <dt class="contTitle" data-function="btn_taskNotAcceptServiceList">'+data.data[0].label+'</dt>\
                 <dd class="contArtitle" data-function="btn_taskNotAcceptServiceList">'+v.detail+'</dd>\
                 <div class="time" data-function="btn_taskNotAcceptServiceList">'+time+'</div>\
                 <div class="contDot" data-function="btn_taskNotAcceptServiceList"></div>\
                 </dl>').appendTo('.taskMainServiceList');*/
            });

        };
        OrderingSystem.getServiceInfoById(options);

    }
    /*
     * 接受的服务详情
     * */
    var acceptSeviceInfo = function(obj,fun){
        var oId="";
        if(obj.parents('dl').data('id')){
            oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }

        var options = {};
        options.SCId=oId;
        options.successFunction = function (dataS) {
            fun();
            //alert(JSON.stringify(data))
            $('#subdealSeverPage').html('');
            var tId =dataS.data[0].TId;
            var time=new Date(dataS.data[0].time.sec*1000);
            time=time.getHours()+':'+time.getMinutes();
            var respondedTime=new Date(dataS.data[0].respondedTime.sec*1000);
            respondedTime=respondedTime.getHours()+':'+respondedTime.getMinutes();

            OrderingSystem.getTableById(tId, function (data) {

                $('<div class="cont">\
                <ul class="contList">\
                <li class="deskNum">\
                    <div></div>\
                    <p>'+data.data[0].label+'</p>\
                </li>\
                <li class="whoCall">\
                <div>联系方式</div>\
                <p>'+data.data[0].label+'</p>\
                </li>\
                    <li class="severInfo">\
                        <div>服务内容</div>\
                        <p class="callPaper">'+dataS.data[0].detail+'</p>\
                        <p class="callTime">'+time+'呼叫服务</p>\
                        <p class="acceptCall">'+respondedTime+'接受服务</p>\
                        <p class="finishCall">'+respondedTime+'完成服务</p>\
                    </li>\
                    <li class="accptOrNot">\
                        <div>接受状态</div>\
                        <p>'+dataS.data[0].clerkList[0].username+'已接受</p>\
                    </li>\
                    <li class="severOrNot">\
                        <div>服务状态</div>\
                        <p>'+dataS.data[0].clerkList[0].username+'已确认</p>\
                    </li>\
                      <li class="finishTask">\
                    <div>处理完成</div>\
                    </li>\
                </ul>\
                </div>').prependTo('#subdealSeverPage');
                /*     $('<dl  data-function="btn_taskNotAcceptServiceList">\
                 <div class="listImg" data-function="btn_taskNotAcceptServiceList"></div>\
                 <dt class="contTitle" data-function="btn_taskNotAcceptServiceList">'+data.data[0].label+'</dt>\
                 <dd class="contArtitle" data-function="btn_taskNotAcceptServiceList">'+v.detail+'</dd>\
                 <div class="time" data-function="btn_taskNotAcceptServiceList">'+time+'</div>\
                 <div class="contDot" data-function="btn_taskNotAcceptServiceList"></div>\
                 </dl>').appendTo('.taskMainServiceList');*/
            });

        };
        OrderingSystem.getServiceInfoById(options);

    }
    /*
     * 点击接受服务按钮
     * */
    var acceptSevice = function(obj,fun){
        var sId = obj.data('id');
        OrderingSystem.respondService(sId, function (data) {
            if(data.error>0){
                $(".wait_gear").hide();
                $('#subcallSeverPage .IndexPage').show();
                $('#subcallSeverPage .sorryIndexMain').show();
            }else{
                var sIdC = sId;
                OrderingSystem.completeService(sIdC, function (data) {

                    setTimeout(function(){fun()},500)
                });

            }
        });
    }
    return{
        notAcceptNewLineItem:function(obj,fun){
            notAcceptNewLineItem(obj,fun);
        },
        printNewLineItem:function(obj,fun){
            printNewLineItem(obj,fun);
        },
        notAcceptServiceInfo:function(obj,fun){
            notAcceptServiceInfo(obj,fun);
        },
        acceptSeviceInfo:function(obj,fun){
            acceptSeviceInfo(obj,fun);
        },

        acceptSevice:function(obj,fun){
            acceptSevice(obj,fun);
        },
        taskServiceList:function(fun){
            taskServiceList(fun);
        }
    }
}();